package com.neusoft.neuiotms.dcam.fixed.converter;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.common.constant.AHConstants;
import com.neusoft.neuiotms.dcam.constant.DCAMConsts;
import com.neusoft.neuiotms.dcam.fixed.dto.AssetDTO;
import com.neusoft.neuiotms.dcam.fixed.model.Asset;
import com.neusoft.neuiotms.dcam.fixed.model.AssetType;
import com.neusoft.neuiotms.dcam.fixed.repository.AssetRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.AssetTypeRepository;
import com.neusoft.neuiotms.dcam.fixed.service.AssetCodeRuleService;
import com.neusoft.neuiotms.dcam.fixed.vo.AssetImportKjVO;
import com.neusoft.neuiotms.dcam.fixed.vo.AssetImportSlVO;
import com.neusoft.neuiotms.dcam.fixed.vo.AssetImportVO;
import com.neusoft.neuiotms.dcam.fixed.vo.ConsumptionImportVO;
import com.neusoft.neuiotms.dcam.workflow.dto.RukuFinanceAssetSLDTO;

@Component
public class AssetConverter extends AbstractConverter<Asset, AssetDTO> {

    @Autowired
    private AssetRepository assetRepository;
    @Autowired
    private AssetCodeRuleService assetCodeRuleService;
    @Autowired
    private AssetTypeRepository assetTypeRepository;

    @Override
    public Asset toModel(final AssetDTO dto) {

        Asset model;
        if (dto.isNew()) {
            model = new Asset();
            if ("3".equals(dto.getCategories())) {
                model.setCode(UUID.randomUUID().toString());
            } else {
                model.setCode(this.assetCodeRuleService.generateAssetCode(dto.getBelongs(), dto.getSmallTypeCode(),
                        dto.getPurchaseDate(), dto.getCategories()));

            }
        } else {
            model = this.assetRepository.getOne(dto.getId());
            model.setCode(dto.getCode());
        }

        model.setName(dto.getName());
        model.setOriginalCode(dto.getOriginalCode());
        model.setPurchaseDate(dto.getPurchaseDate());
        model.setSupplierId(dto.getSupplierId());
        model.setSupplierName(dto.getSupplierName());
        model.setSpecifications(dto.getSpecifications());
        model.setSerialnumber(dto.getSerialnumber());
        model.setComputerRoom(dto.getComputerRoom());
        model.setLocationName(dto.getLocationName());
        model.setCabinet(dto.getCabinet());
        model.setMaintenanceStartTime(dto.getMaintenanceStartTime());
        model.setMaintenanceEndTime(dto.getMaintenanceEndTime());
        model.setLocation(dto.getLocation());
        model.setBigTypeName(dto.getBigTypeName());
        model.setBigTypeCode(dto.getBigTypeCode());
        model.setMiddleTypeName(dto.getMiddleTypeName());
        model.setMiddleTypeCode(dto.getMiddleTypeCode());
        model.setSmallTypeName(dto.getSmallTypeName());
        model.setSmallTypeCode(dto.getSmallTypeCode());
        final AssetType assetType = this.assetTypeRepository.findByCode(dto.getSmallTypeCode());
        if ("3".equals(dto.getCategories()) && (null != assetType)) {
            model.setMeasureUnit(assetType.getMeasureUnit());
        } else {
            model.setMeasureUnit(dto.getMeasureUnit());
        }
        model.setContractNo(dto.getContractNo());
        model.setContractName(dto.getContractName());
        model.setBrand(dto.getBrand());
        model.setAmount(dto.getAmount());
        model.setMoney(dto.getMoney());
        model.setUsePeriod(dto.getUsePeriod());
        //        model.setAgent(dto.getAgent());
        model.setNote(dto.getNote());
        model.setManagementOrgName(dto.getManagementOrgName());
        model.setManagementOrgCode(dto.getManagementOrgCode());
        model.setManagementUserCode(dto.getManagementUserCode());
        model.setManagementUserName(dto.getManagementUserName());
        model.setBelongs(dto.getBelongs());
        model.setCategories(dto.getCategories());
        model.setCard(dto.getCard());
        model.setLocked(dto.getLocked());
        model.setStatus(dto.getStatus());
        model.setPurpose(dto.getPurpose());

        return model;
    }

    @Override
    public AssetDTO toDTO(final Asset model, final boolean forListView) {

        final AssetDTO dto = new AssetDTO();

        dto.setId(model.getId());
        dto.setName(model.getName());
        dto.setCode(model.getCode());
        dto.setOriginalCode(model.getOriginalCode());
        dto.setPurchaseDate(model.getPurchaseDate());
        dto.setSupplierId(model.getSupplierId());
        dto.setSupplierName(model.getSupplierName());
        dto.setSpecifications(model.getSpecifications());
        dto.setSerialnumber(model.getSerialnumber());
        dto.setComputerRoom(model.getComputerRoom());
        dto.setLocationName(model.getLocationName());
        dto.setCabinet(model.getCabinet());
        dto.setMaintenanceStartTime(model.getMaintenanceStartTime());
        dto.setMaintenanceEndTime(model.getMaintenanceEndTime());
        dto.setLocation(model.getLocation());
        dto.setBigTypeName(model.getBigTypeName());
        dto.setBigTypeCode(model.getBigTypeCode());
        dto.setMiddleTypeName(model.getMiddleTypeName());
        dto.setMiddleTypeCode(model.getMiddleTypeCode());
        dto.setSmallTypeName(model.getSmallTypeName());
        dto.setSmallTypeCode(model.getSmallTypeCode());
        dto.setContractNo(model.getContractNo());
        dto.setContractName(model.getContractName());
        dto.setMeasureUnit(model.getMeasureUnit());
        dto.setBrand(model.getBrand());
        dto.setAmount(model.getAmount());
        dto.setMoney(model.getMoney());
        dto.setUsePeriod(model.getUsePeriod());
        //        dto.setAgent(model.getAgent());
        dto.setNote(model.getNote());
        dto.setManagementOrgName(model.getManagementOrgName());
        dto.setManagementOrgCode(model.getManagementOrgCode());
        dto.setManagementUserCode(model.getManagementUserCode());
        dto.setManagementUserName(model.getManagementUserName());
        dto.setBelongs(model.getBelongs());
        dto.setCategories(model.getCategories());
        dto.setCard(model.getCard());
        dto.setLocked(model.getLocked());
        dto.setStatus(model.getStatus());
        dto.setPurpose(model.getPurpose());
        dto.setLabelPrinted(model.getLabelPrinted());

        return dto;
    }

    /**
     * 资产编码，位置、大类型、中类型、小类型、使用部门、使用人，不在这里处理
     *
     * @param vo
     * @return
     */
    public Asset toModel(final AssetImportVO vo) {
        Asset model = null;
        if (StringUtils.isNoneEmpty(vo.getCode())) {
            model = this.assetRepository.findByCode(vo.getCode());
            if (null == model) {
                model = new Asset();
                model.setCode(vo.getCode());
            }
        } else {
            model = new Asset();
        }

        model.setBelongs(vo.getBelongs());
        model.setCategories(vo.getCategories());
        if (vo instanceof AssetImportSlVO) {
            model.setOriginalCode(((AssetImportSlVO) vo).getOriginalCode());
        } else if (vo instanceof AssetImportKjVO) {
            model.setOriginalCode(((AssetImportKjVO) vo).getOriginalCode());
        }
        model.setName(vo.getName());

        model.setPurchaseDate(vo.getPurchaseDate());
        model.setBrand(vo.getBrand());
        model.setSupplierName(vo.getManufactor());
        model.setSpecifications(vo.getSpecifications());
        model.setSerialnumber(vo.getSerialnumber());
        if (vo instanceof AssetImportKjVO) {
            model.setComputerRoom(((AssetImportKjVO) vo).getComputerRoom());
            model.setCabinet(((AssetImportKjVO) vo).getCabinet());
        }

        model.setMaintenanceStartTime(vo.getMaintenanceStartDate());
        model.setMaintenanceEndTime(vo.getMaintenanceEndDate());
        model.setContractNo(vo.getContract());
        model.setMeasureUnit(vo.getMeasureUnit());
        model.setMoney(vo.getMoney());
        model.setAmount(vo.getAmount());
        model.setUsePeriod(vo.getMonth());
        model.setNote(vo.getNote());
        model.setUpdateTime(new Date());
        model.setPurpose(vo.getPurpose());
        return model;
    }

    public Asset toModel(final ConsumptionImportVO vo) {
        final Asset model = new Asset();
        model.setBelongs(vo.getBelongs());
        model.setCategories(vo.getCategories());
        model.setName(vo.getName());
        model.setPurchaseDate(vo.getPurchaseDate());
        model.setBrand(vo.getBrand());
        model.setSpecifications(vo.getSpecifications());
        //        model.setMeasureUnit(vo.getMeasureUnit());
        model.setMoney(vo.getMoney());
        model.setNote(vo.getNote());
        model.setUpdateTime(new Date());
        model.setAmount(vo.getAmount());
        model.setPurpose(vo.getPurpose());
        model.setStatus("unused");
        if (StringUtils.isNotBlank(vo.getLocationName())) {
            final String[] segments = vo.getLocationName().split(DCAMConsts.DICTIONARY_SPLITOR);
            if (segments.length == 2) {
                model.setLocation(segments[1]);
                model.setLocationName(segments[0]);
            }
        }
        if (StringUtils.isNotBlank(vo.getAssetTypeName())) {
            final String[] segments = vo.getAssetTypeName().split(DCAMConsts.DICTIONARY_SPLITOR);
            if (segments.length == 2) {
                model.setSmallTypeCode(segments[1]);
                model.setSmallTypeName(segments[0]);
            }
        }
        return model;
    }

    public List<Asset> toModels(final RukuFinanceAssetSLDTO sa) {
        // 计算单价
        final BigDecimal decimal = new BigDecimal(sa.getMoney() / sa.getAmount());
        final Double sMoney = decimal.setScale(2, BigDecimal.ROUND_HALF_DOWN).doubleValue();

        final List<Asset> assets = Lists.newArrayList();
        for (int i = 0; i < sa.getAmount(); i++) {
            final Asset model = new Asset();
            model.setBelongs(AHConstants.BELONG_SL_CODE);
            if ("1601".equals(sa.getBigTypeCode())) {
                model.setCategories("1");
            } else {
                model.setCategories("2");
            }
            model.setPurchaseDate(sa.getPurchaseDate());
            model.setMaintenanceStartTime(sa.getMaintenanceStartTime());
            model.setMaintenanceEndTime(sa.getMaintenanceEndTime());
            model.setCode(this.assetCodeRuleService.generateAssetCode(model.getBelongs(), sa.getSmallTypeCode(),
                    model.getPurchaseDate(), model.getCategories()));

            model.setName(sa.getName());
            model.setBigTypeCode(sa.getBigTypeCode());
            model.setBigTypeName(sa.getBigTypeName());
            model.setMiddleTypeCode(sa.getMiddleTypeCode());
            model.setMiddleTypeName(sa.getMiddleTypeName());
            model.setSmallTypeCode(sa.getSmallTypeCode());
            model.setSmallTypeName(sa.getSmallTypeName());
            model.setMaintenanceStartTime(sa.getMaintenanceStartTime());
            model.setBrand(sa.getBrand());
            model.setSpecifications(sa.getSpecifications());
            model.setSerialnumber(sa.getSerialnumber());
            model.setMeasureUnit(sa.getMeasureUnit());
            //            model.setCard(sa.getCard());
            model.setOriginalCode(sa.getCard());
            model.setUsePeriod(sa.getUsePeriod());

            model.setUpdateTime(new Date());
            model.setAmount(1);
            model.setStatus("unused");

            model.setNote(sa.getNote());
            model.setManagementOrgCode(sa.getManagementOrgCode());
            model.setManagementOrgName(sa.getManagementOrgName());
            model.setManagementUserCode(sa.getManagementUserCode());
            model.setManagementUserName(sa.getManagementUserName());
            model.setMoney(sMoney);
            model.setPurpose(sa.getPurpose());

            assets.add(model);

        }
        return assets;
    }

    public AssetImportVO toVO(final Asset model) {
        final AssetImportVO vo =
                AHConstants.BELONG_KJ_CODE.equals(model.getBelongs()) ? new AssetImportKjVO() : new AssetImportSlVO();
        vo.setBelongs(model.getBelongs());
        vo.setCategories(model.getCategories());
        vo.setName(model.getName());
        vo.setCode(model.getCode());
        vo.setPurchaseDate(model.getPurchaseDate());
        vo.setBrand(model.getBrand());
        vo.setManufactor(model.getSupplierName());
        vo.setSpecifications(model.getSpecifications());
        vo.setSerialnumber(model.getSerialnumber());
        if (vo instanceof AssetImportKjVO) {
            ((AssetImportKjVO) vo).setOriginalCode(model.getOriginalCode());
            ((AssetImportKjVO) vo).setComputerRoom(model.getComputerRoom());
            ((AssetImportKjVO) vo).setCabinet(model.getCabinet());
        } else if (vo instanceof AssetImportSlVO) {
            ((AssetImportSlVO) vo).setOriginalCode(model.getOriginalCode());
        }
        vo.setMaintenanceStartDate(model.getMaintenanceStartTime());
        vo.setMaintenanceEndDate(model.getMaintenanceEndTime());
        vo.setContract(model.getContractNo());
        vo.setMeasureUnit(model.getMeasureUnit());
        vo.setMoney(model.getMoney());
        vo.setAmount(model.getAmount());
        vo.setMonth(model.getUsePeriod());
        vo.setPurpose(model.getPurpose());
        vo.setNote(model.getNote());

        vo.setLocationName(model.getLocation());

        vo.setAssetTypeName1(model.getBigTypeName() + DCAMConsts.DICTIONARY_SPLITOR + model.getBigTypeCode());
        vo.setAssetTypeName2(model.getMiddleTypeName() + DCAMConsts.DICTIONARY_SPLITOR + model.getMiddleTypeCode());
        vo.setAssetTypeName(model.getSmallTypeName() + DCAMConsts.DICTIONARY_SPLITOR + model.getSmallTypeCode());
        if (StringUtils.isNotBlank(model.getManagementOrgName())
                && StringUtils.isNotBlank(model.getManagementOrgCode())) {
            vo.setOrg(model.getManagementOrgName() + DCAMConsts.DICTIONARY_SPLITOR + model.getManagementOrgCode());
        }
        vo.setUser(model.getManagementUserName());
        vo.setStatus(model.getStatus());
        return vo;
    }

    public ConsumptionImportVO toConsumptionVO(final Asset model) {
        final ConsumptionImportVO vo = new ConsumptionImportVO();
        vo.setBelongs(model.getBelongs());
        vo.setCategories(model.getCategories());
        vo.setName(model.getName());
        vo.setPurchaseDate(model.getPurchaseDate());
        vo.setBrand(model.getBrand());
        vo.setSpecifications(model.getSpecifications());
        vo.setMeasureUnit(model.getMeasureUnit());
        vo.setMoney(model.getMoney());
        vo.setAmount(model.getAmount());
        vo.setPurpose(model.getPurpose());
        vo.setNote(model.getNote());
        if (StringUtils.isNotBlank(model.getSmallTypeName())
                && StringUtils.isNotBlank(model.getSmallTypeCode())) {
            vo.setAssetTypeName(model.getSmallTypeName() + DCAMConsts.DICTIONARY_SPLITOR + model.getSmallTypeCode());
        }
        if (StringUtils.isNotBlank(model.getLocation())
                && StringUtils.isNotBlank(model.getLocationName())) {
            vo.setLocationName(model.getLocationName() + DCAMConsts.DICTIONARY_SPLITOR + model.getLocation());
        }
        return vo;
    }
}
